﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PublicCommon;
using BLL;
using WinFormQuickQureyInterface;
using ORMapping;
using MForm;

namespace ModifyDataMoudle
{
    public partial class CardForm : MdiForm,IQuickQueryInterface
    {
        private TBLCardTypeBLL tctb = new TBLCardTypeBLL();
        private readonly Image nullImage = new Bitmap(1, 1);
        private Int32 DefRowTemplateHight;
        private readonly String TableName = "IntegrationView";
        //private Int16 PhotoColumnIndex;
        public CardForm(Parent im) : base(im)
        {
            InitializeComponent();
            HYDataGridView.Helper.InitDataGridViewColumn(String.Format("{0}_{1}", this.Name, this.dgvCard.Name), ref dgvCard);
            //PhotoColumnIndex = GetPhotoColumnIndex();
            dgvCard.AutoGenerateColumns = false;
            IList<TBLCardType> ilSource = tctb.GetAllCardTypes();
            ilSource.Insert(0, new TBLCardType { CardType = string.Empty, CardName = string.Empty });
            CardType.DataSource = ilSource;
            IList<CardVilad> ilCV = new List<CardVilad>();
            ilCV.Insert(0, new CardVilad { CardViladName = "所有", CardViladValue = " C.Valid = 'Y' " });
            //SQLite的现在时间DATETIME('NOW')
            ilCV.Insert(1, new CardVilad { CardViladName = "有效", CardViladValue = " DATETIME('NOW') between C.StartDate and C.EndDate and C.Valid = 'Y' " });
            ilCV.Insert(2, new CardVilad { CardViladName = "无效", CardViladValue = " DATETIME('NOW') >= C.EndDate or C.Valid = 'N' " });
            Card_Valid.DataSource = ilCV;
            DefRowTemplateHight = dgvCard.RowTemplate.Height;
        }

        //private Int16 GetPhotoColumnIndex()
        //{
        //    if (dgvCard.Columns.Contains("IntegrationView_Photo"))
        //    {
        //        System.Collections.IEnumerator i = dgvCard.Columns.GetEnumerator();
        //        Int16 Index = 0;
        //        while (i.MoveNext()) {
        //            DataGridViewColumn d = i.Current as DataGridViewColumn;
        //            if (d.Name == "IntegrationView_Photo")
        //            {
        //                return Index;
        //            }
        //            ++Index;
        //        }
        //    }
        //    return -1;
        //}
        
        private void btnQuery_Click(object sender, EventArgs e)
        {
            Query();
        }
        
        //private void SetShowPhoto()
        //{
        //    if(PhotoColumnIndex == -1 || dgvCard.Columns.Count == 0)
        //    {
        //        return;
        //    }
        //    if(ShowPhoto.Checked)
        //    {
        //        dgvCard.RowTemplate.Height = 223;
        //        dgvCard.Columns[PhotoColumnIndex].Visible = true;
        //        dgvCard.Refresh();
        //        return;
        //    }
        //    dgvCard.RowTemplate.Height = DefRowTemplateHight;
        //    dgvCard.Columns[PhotoColumnIndex].Visible = false;
        //    dgvCard.Refresh();
        //}

        public override void Query()
        {
            var cdb = new IntegrationViewBLL();
            dgvCard.Columns.Clear();
            HYDataGridView.Helper.InitDataGridViewColumn(String.Format("{0}_{1}", this.Name, this.dgvCard.Name), ref dgvCard);
            //SetShowPhoto();
            var DataSource = cdb.GetDataGridViewShowCards(this.Name,this.Controls,Card_Valid.SelectedValue.ToString());
            dgvCard.DataSource = DataSource;
            HYDataGridView.Helper.SetUsefulLifeOverWarning(TableName, ref this.dgvCard);
            CardCount.Text = String.Concat("记录数:", DataSource == null ? "0" : DataSource.Rows.Count.ToString());
        }


        private void csmModify_Click(object sender, EventArgs e)
        {
            DataRow drCardInfo = HYDataGridView.Helper.GetDataGridViewSelectedRow(this,dgvCard);
            if (null == drCardInfo)
            {
                return;
            }
            //int? Position = HYDataGridView.Helper.GetDataGridViewSelectedPosition(this, dgvCard);
            drCardInfo = GetCardInfoByCardNo(drCardInfo);
            //传递初始化数据
            var frm = new ModifyCardInfoForm(ref drCardInfo);
            frm.IsNewInfo = false;
            frm.Text = "修改攀岩卡信息";
            frm.ShowDialog();
            HYDataGridView.Helper.SetUsefulLifeOverWarning(TableName,ref this.dgvCard);
            //Query();
            //HYDataGridView.Helper.SetDataGridViewSelectedPosition(this, dgvCard,Position);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewCardInfo();
        }

        private void csmNew_Click(object sender, EventArgs e)
        {
            NewCardInfo();
        }

        private void NewCardInfo()
        {
            ModifyCardInfoForm frm = new ModifyCardInfoForm();
            frm.Text = "新建攀岩卡信息";
            frm.ShowDialog();
        }

        #region IQuickQueryInterface 成员

        object IQuickQueryInterface.GetQuickQueryCondition()
        {
            DataRow drCard = HYDataGridView.Helper.GetDataGridViewSelectedRow(this, dgvCard);
            if (null == drCard)
            {
                return null;
            }
            ORMapping.Card cd = new ORMapping.Card();
            NHibernateHelper.Convert.DataRowConvertToObject(cd, drCard);
            return cd;
        }

        void IQuickQueryInterface.QuickQuery(object oQuickQueryCondition)
        {
            Control.ControlCollection ccc = this.Controls;
            Functions.SetQueryControlText(ref ccc, oQuickQueryCondition);
            Query();
        }

        #endregion

        private void dgvCard_DoubleClick(object sender, EventArgs e)
        {
            ShowMemberInfo();
        }

        private void csmMemberInfo_Click(object sender, EventArgs e)
        {
            ShowMemberInfo();
        }

        private void ShowMemberInfo()
        {
            DataRow drTarget = HYDataGridView.Helper.GetDataGridViewSelectedRow(this, dgvCard);
            if (null == drTarget)
            {
                MessageBox.Show("没有选择需要显示的行");
                return;
            }
            //int? Position = HYDataGridView.Helper.GetDataGridViewSelectedPosition(this, dgvCard);
            drTarget = GetMemberInfoById(drTarget); 
            var frm = new ModifyMemberInfoForm(ref drTarget, true);
            frm.IsNewInfo = false;
            frm.Text = "修改会员信息";
            frm.ShowDialog();
            //Query();
            //HYDataGridView.Helper.SetDataGridViewSelectedPosition(this, dgvCard, Position);
        }

        private DataRow GetMemberInfoById(DataRow drCardInfo)
        {
            var mBll = new MemberInfoBLL();
            var DT = NHibernateHelper.Convert.CreateDataTableByT<MemberInfo>();
            var r = DT.NewRow();
            NHibernateHelper.Convert.ObjectConvertToDataRow(mBll.GetMemberInfoByID(Int32.Parse(drCardInfo["IntegrationView_MemberID"].ToString())),ref r);
            return r;
        }

        private DataRow GetCardInfoByCardNo(DataRow drCardInfo)
        {
            var DT = NHibernateHelper.Convert.CreateDataTableByT<Card>();
            var r = DT.NewRow();
            var cdBll = new CardBLL();
            NHibernateHelper.Convert.ObjectConvertToDataRow(cdBll.QueryCardByNO(drCardInfo["IntegrationView_CardNO"].ToString()),ref r);
            return r;
        }

        private void csmHistory_Click(object sender, EventArgs e)
        {
            DataRow drTarget = HYDataGridView.Helper.GetDataGridViewSelectedRow(this, dgvCard);
            if (null == drTarget)
            {
                MessageBox.Show("没有选择需要显示的行");
                return;
            }
            drTarget = GetCardInfoByCardNo(drTarget);
            var cd = new ORMapping.Card();
            NHibernateHelper.Convert.DataRowConvertToObject(cd, drTarget);            
            var chf = new CardHistoryForm(cd);
            chf.ShowDialog();
        }

        private void dgvCard_Sorted(object sender, EventArgs e)
        {
            HYDataGridView.Helper.SetUsefulLifeOverWarning(TableName,ref this.dgvCard);
        }
                       
        void DgvCardCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //if(PhotoColumnIndex != -1 && PhotoColumnIndex == e.ColumnIndex)
            //{
            //    DataGridViewImageCell c = dgvCard[e.ColumnIndex,e.RowIndex] as DataGridViewImageCell;
            //    if (c.Value == DBNull.Value)
            //    {
            //        c.Value = nullImage;
            //    }
            //}
        }
        
        //void ShowPhotoCheckedChanged(object sender, EventArgs e)
        //{
        //    SetShowPhoto();
        //}
    }
}